package com.nineclock.auth.config;

import com.nineclock.auth.exception.NcWebResponseExceptionTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import javax.sql.DataSource;

//@EnableAuthorizationServer  开启 认证授权服务
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
	
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private DataSource dataSource;
  
    //配置客户端的详情信息
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//        clients.inMemory()
//                //配置客户端的标识
//                .withClient("nc_client_app")
//                .secret(passwordEncoder.encode("123456"))
//                .scopes("all")
//                .redirectUris("http://localhost")
//                .authorizedGrantTypes("authorization_code","password")
//                .and()
//                .withClient("nc_client_pc")
//                .secret(passwordEncoder.encode("123456"))
//                .scopes("all")
//                .redirectUris("http://localhost")
//                .authorizedGrantTypes("authorization_code","password");
        clients.withClientDetails(clientDetailsService());
    }


    @Bean
    public ClientDetailsService clientDetailsService() {
        return  new JdbcClientDetailsService(dataSource);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("permitAll") //开放 /oauth/token
                .checkTokenAccess("permitAll")  //开放： /oauth/authorize
                .allowFormAuthenticationForClients(); //开启表单认证方式
    }

    @Autowired
    AuthenticationManager authenticationManager;
    
	
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.exceptionTranslator(new NcWebResponseExceptionTranslator()) //配置异常处理
                .authenticationManager(authenticationManager) //认证管理器
                .tokenServices(tokenServices()) // token配置信息
                .allowedTokenEndpointRequestMethods(HttpMethod.POST); //表单的请求方式
    }


    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired
    TokenStore tokenStore;


    @Autowired
    JwtAccessTokenConverter tokenConverter;
    @Bean
    public AuthorizationServerTokenServices tokenServices(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        //设置 客户端详情
        tokenServices.setClientDetailsService(clientDetailsService);
        //设置令牌存储
        tokenServices.setTokenStore(tokenStore);
        //设置token有效期
        tokenServices.setAccessTokenValiditySeconds(60 * 60 * 12);
        //设置刷新 token 的有效期
        tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 30);
        //token 的增强配置
        tokenServices.setTokenEnhancer(tokenConverter);
        return tokenServices;
    }


}